//
// Created by hkkzzxz on 24-8-31.
//

#include "LogManager.h"

void LogManager::init(const std::string &logFilePath, spdlog::level::level_enum level) {
    // 设置异步队列的容量
    size_t q_size = 8192;
    // 设置线程数量
    size_t thread_count = 1;
    // 初始化异步队列
    spdlog::init_thread_pool(q_size, thread_count);

    // 创建控制台日志器
    consoleLogger = std::make_shared<spdlog::logger>("console",
                                                     std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
    consoleLogger->set_level(level);
    consoleLogger->set_pattern("[console] [%Y-%m-%d %H:%M:%S] [%^%l%$] %v");

    // 创建文件日志器
    fileLogger = std::make_shared<spdlog::logger>("file",
                                                  std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilePath,
                                                                                                      true));
    fileLogger->set_level(level);
    fileLogger->set_pattern("[file] [%Y-%m-%d %H:%M:%S] [%^%l%$] %v");

    // 注册日志器
    spdlog::register_logger(consoleLogger);
    spdlog::register_logger(fileLogger);
}

std::shared_ptr<spdlog::logger> LogManager::getConsoleLogger() {
    return consoleLogger;
}

std::shared_ptr<spdlog::logger> LogManager::getFileLogger() {
    return fileLogger;
}

void LogManager::shutdown() {
    spdlog::shutdown();
}
